/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.base
{

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

/**
 * Base class for all controls. A control is user visible element that
 * she can drag to edit the image.
 */
public class Control extends Sprite
{

	protected var stateManager:StateManager
	
	function Control(stateManager:StateManager) {
		this.stateManager = stateManager
	}
	
	//--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------			
	/**
	 * Cursor belonging to this control
	 */
	protected var cursor:Cursor

	//--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------			
	/**
	 * Initiate the control.
	 */
	public function init(toolsComponent:Sprite): void
	{
		addEventListener(MouseEvent.MOUSE_DOWN, startInteractionHandler)
		toolsComponent.addChild(this)
		
		// show and hide cursor for control
		if (cursor) {
			addEventListener(MouseEvent.MOUSE_OVER, showCursor)
			addEventListener(MouseEvent.MOUSE_OUT, hideCursor)
			cursor.visible = false
			addChild(cursor)
		}		
		
		draw()
	}
	
	/**
	 * Show the cursor of this control.
	 */
	private function showCursor(mouseEvent:MouseEvent): void
	{
		moveCursor(mouseEvent)
		cursor.visible = true
		addEventListener(MouseEvent.MOUSE_MOVE, moveCursor)
	}

	/**
	 * Hide the cursor of this control.
	 */	
	private function hideCursor(mouseEvent:MouseEvent): void
	{
		cursor.visible = false
		removeEventListener(MouseEvent.MOUSE_MOVE, moveCursor)
	}
	
	/**
	 * Stick the visible cursor to the mouse pointer.
	 */		
	private function moveCursor(mouseEvent:MouseEvent): void
	{
		cursor.x = mouseEvent.localX + cursor.offset_x
		cursor.y = mouseEvent.localY + cursor.offset_y 
	}
	
	protected function startInteractionHandler(mouseEvent:MouseEvent):void {
		stateManager.interactionStarts()
	}
	
	/**
	 * Draw the control.
	 */
	protected function draw( event:Event=null ): void 
	{
	}
}
}